package com.zxn.xiaoran.presenter;

import com.zcommon.lib.SystemUtils;
import com.zxn.presenter.presenter.IView;
import com.zxn.xiaoran.XRApp;
import com.zxn.xiaoran.model.api.ApiFactory;
import com.zxn.xiaoran.model.bean.CheckInfo;
import com.zxn.xiaoran.model.bean.XMInfo;


import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by zxn on 2019/5/8.
 */
public class AppViewPresenter extends XMPresenter<AppViewPresenter.IAppView> implements IXMPresenter.IAppView {


    private XMInfo mXMInfo;
    private CheckInfo mCheckInfo;
    private int mBuildVersionNo;

    @Override
    public void appView() {
//        HashMap<String, String> parameters = initParameters();
//        parameters.put("", "");
//        putSign(parameters);
        ApiFactory.getInstance().getXMApi().appView("01f43f783df1040982840643d1b3e46c", "bc535eb38db078ea88ec25f3ea2e4ca8").subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new XMObserver<XMInfo>() {

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(XMInfo info) {
                        //showToast("请求成功.");
                        mXMInfo = info;
                    }

                });
    }

    @Override
    public void appCheck() {
        ApiFactory.getInstance().getXMApi().appCheck("01f43f783df1040982840643d1b3e46c", "bc535eb38db078ea88ec25f3ea2e4ca8").subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new XMObserver<CheckInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }
                    @Override
                    public void onNext(CheckInfo info) {
                        //showToast("请求成功.");
                        mCheckInfo = info;
                        getView().onAppCheck();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        //getView().onAppCheck();
                    }
                });
    }

    public XMInfo getXMInfo() {
        return mXMInfo;
    }

    public CheckInfo getCheckInfo() {
        return mCheckInfo;
    }

//    public CheckInfo.DataBean getCheckInfoData() {
//        return mCheckInfo.data;
//    }

    public interface IAppView extends IView {
//        void onAppView();

        void onAppCheck();
    }

    public boolean hasNewVersion() {
        if (mCheckInfo == null || mCheckInfo.data == null) {
            return false;
        }
        try {
            mBuildVersionNo = Integer.parseInt(mCheckInfo.data.buildVersionNo);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        return mBuildVersionNo > SystemUtils.getVersionCode(XRApp.getApplication());
    }
}
